package net.halflite.mzkr.action;

import java.io.IOException;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import net.halflite.mzkr.dto.user.ImageDto;
import net.halflite.mzkr.service.UserImageService;
import net.halflite.mzkr.type.ImageSizeType;

import org.seasar.struts.annotation.Execute;
import org.seasar.struts.annotation.LongType;
import org.seasar.struts.annotation.Mask;
import org.seasar.struts.annotation.Required;

/** アイコン表示用アクションクラス */
public class ImageAction {

	@Resource
	private HttpServletResponse response;

	@Resource
	private UserImageService userImageService;

	@Required
	@Mask(mask="[0-9a-zA-Z_]+")
	public String username;
	
	@Required
	@Mask(mask="large|small|icon")
	public String size;
	
	@Required
	@LongType
	public String userImageId;
	
	
	/** 
	 * 画像表示 
	 * @throws IOException 
	 */
    @Execute(urlPattern = "{username}/{size}/{userImageId}", input = "notfound")
    public String index() throws IOException {
    	long imageId = Long.valueOf(userImageId);
    	ImageSizeType sizeType = ImageSizeType.toType(size);
    	ImageDto dto = userImageService.findImage(username, sizeType, imageId);
    	if (dto == null) {
    		response.setStatus(404);
    	} else {
    		dto.setResponse(response);
    	}
    	return null;
    }
    
    @Execute(validator = false)
    public String notfound() {
    	response.setStatus(404);
    	return null;
    }
}
